import { fileURLToPath, URL } from 'node:url'

import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import DefineOptions from 'unplugin-vue-define-options/vite';

// https://vitejs.dev/config/
export default ({ command, mode }) => {

  let { VITE_PUBLIC_PATH } = loadEnv(mode, process.cwd());

  return defineConfig({
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      DefineOptions(),
      /*importToCDN({
        modules: [
          {
            name: 'vue',
            var: 'Vue',
            path: 'https://cdn.jsdelivr.net/npm/vue@3.5.10/dist/vue.global.min.js'
          },
          {
            name: 'element-plus', // 依赖名称,
            var: 'ElementPlus', // 全局变量名
            path: 'https://cdn.jsdelivr.net/npm/element-plus@2.8.4/dist/index.full.min.js', // CDN 链接
            css: 'https://cdn.jsdelivr.net/npm/element-plus@2.8.4/dist/index.min.css' // 依赖有css就填，没有就去掉这个
          },
          {
            name: 'axios',
            var: 'axios',
            path: 'https://cdn.jsdelivr.net/npm/axios@1.7.7/dist/axios.min.js'
          },
          {
            name: 'echarts',
            var: 'echarts',
            path: 'https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js'
          },
          {
            name: 'lodash',
            var: '_',
            path: 'https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js'
          }
        ]
      })*/
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    base: VITE_PUBLIC_PATH,
    minify: true,
    build: {
      target: [ 'es2015' ],
      rollupOptions: {
        // 确保外部化处理那些你不想打包进库的依赖
        /*external: [ 'vue', 'axios', 'lodash', 'element-plus', 'element-plus/dist/index.css' ], // 示例：如果是 Vue 库，可以将 Vue 外部化
        output: {
          globals: {
            vue: 'Vue', // 如果外部化了 Vue，这里提供其全局变量名
            axios: 'axios',
            lodash: '_',
            'element-plus': 'ElementPlus'
          },
        },*/
      },
    },
    server: {
      host: '0.0.0.0',
      port: 3000,
      open: true,
      proxy: {
        '/api': {
          // target: 'http://jdyb.v5.juzhile.cc',
          target: 'http://localhost:8000',
          // target: 'http://v5.juzhile.cc',
          changeOrigin: true,
          headers: {
            Referer: 'http://v5.juzhile.cc'
          }
          // rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    }
  })
}
